using JT808Protocol.Common;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace JT808Protocol.Models.BodyStrategy.Authenticate
{
    /// <summary>
    /// 0100终端注册消息体
    /// </summary>
    public class DrRegisterDev_0x0100 : IJT808Body
    {
        /// <summary>
        /// 省域ID
        /// </summary>
        [JsonPropertyName("省域ID")] public string ProvinceID { get; set; }

        /// <summary>
        /// 市县域ID
        /// </summary>
        [JsonPropertyName("市县域ID")] public string CityID { get; set; }

        /// <summary>
        /// 制造商ID
        /// </summary>
        [JsonPropertyName("制造商ID")] public string ManufacturerID { get; set; }

        /// <summary>
        /// 终端型号
        /// </summary>
        [JsonPropertyName("终端型号")] public string DeviceModelNum { get; set; }

        /// <summary>
        /// 终端ID
        /// </summary>
        [JsonPropertyName("终端ID")] public string DeviceID { get; set; }

        /// <summary>
        /// 车辆颜色
        /// </summary>
        [JsonPropertyName("车辆颜色")] public byte VehicleColor { get; set; }

        /// <summary>
        /// 车辆标识
        /// 车牌颜色为 0 时，表示车辆 VIN；否则，表示公安交通管理部门颁发的机动车号牌。
        /// </summary>
        [JsonPropertyName("车辆标识")] public string LicenceNumber { get; set; }

        public override IJT808Body ReadBody(ReadOnlySpan<byte> source, ref int index)
        {
            //省域ID
            ProvinceID = ParseHelper.AsciiString(ref source, index, 2);
            index += 2;

            //市县域ID
            CityID = ParseHelper.AsciiString(ref source, index, 2);
            index += 2;

            var mLen = 0; //制造商ID长度
            var dLen = 0; //终端型号长度
            var dLen_2 = 0; //终端ID长度
            if (HeaderContext.ProtocolVersion == JT808ProtocolVersion.v2019)
            {
                mLen = 11;
                dLen = 30;
                dLen_2 = 30;
            }
            else if (HeaderContext.ProtocolVersion == JT808ProtocolVersion.v2013)
            {
                mLen = 5;
                dLen = 20;
                dLen_2 = 7;
            }

            //制造商ID
            ManufacturerID = ParseHelper.Gb2312String(ref source, index, mLen);
            index += mLen;

            //终端型号
            DeviceModelNum = ParseHelper.Gb2312String(ref source, index, dLen);
            index += dLen;

            //终端ID
            DeviceID = ParseHelper.Gb2312String(ref source, index, dLen_2);
            index += dLen_2;

            //车牌颜色
            VehicleColor = source[index];
            index += 1;

            // 车牌
            LicenceNumber = ParseHelper.Gb2312String(ref source, index, source.Length - index - 2);

            return this;
        }

        public override List<byte> WriteToBytes()
        {
            throw new NotImplementedException();
        }
    }
}